/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

/**
 *
 * @author meydlo
 */
import javaapplication6.ControlLayer.WorkerControl;
import javaapplication6.DomainLayer.Worker;
public class WorkerUI {

    private WorkerControl workerControl;
    private Listener listener=new Listener();


    public WorkerUI(){
        workerControl=new WorkerControl();
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) create worker"+
                "\n (2) print worker info"+
                "\n (3) update worker"+
                "\n (4) delete worker"+
                "\n (5) set Discount for employees"+
                "\n (6) print workerList"+
                "\n (7) main menu"
                );
    }
    public void WorkerManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    createWorker();
                    break;
                }
                case 2:{
                    printWorkerInfo();
                    break;
                }
                case 3:{
                    updateWorker();
                    break;
                }
                case 4:{
                    deleteWorker();
                    break;
                }
                case 5:{
                    setDiscountToWorkers();
                    break;
                }
                case 6:{
                    printWorkerList();
                    break;
                }
                case 7:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-7");
                }
            }
        }
    }

    public void createWorker()
    {
        String name=getNameUI();
        String password=getPhoneNoUI();
        String address=getAddressUI();
        String phoneNo=getPhoneNoUI();
        double salary=getSalaryUI();

        int type=setTypeAndReturn();

        workerControl.createWorker(name, phoneNo, address, salary, type, password);
        System.out.println("Employee successful registered");
    }

    protected Worker getWorkerByIDui()
    {
        System.out.println("write ID of the worker: ");
        long num=listener.getLineInt();
        return workerControl.findWorker(num);
    }

    public void printWorkerInfo()
    {
        try{
            Worker worker=getWorkerByIDui();
            worker.printWorkerInfo();
        }
        catch(Exception e){
        }
    }

    public void printWorkerList()
    {
        workerControl.printWorkerList();
    }

    public void printChoiceOfChanges()
    {
        System.out.println("\n(1) name: "+
                "\n(2) address: "+
                "\n(3) phoneNo: "+
                "\n(4) password: "+
                "\n(5) salary: "+
                "\n(6) exit"
                );
    }

    public void deleteWorker()
    {
        System.out.println("write ID of the worker: ");
        long id=listener.getLineInt();
        workerControl.deleteWorker(id);
    }

    //setting type of user till choose option (1 || 2 || 3);
    public int setTypeAndReturn()
    {
     printType();
     int type=0;
     do{
         type=listener.getLineInt();
     }while(((0>=type)||(type>3)));
     return type;
    }

    public void printType()
    {
        System.out.println("choose one of the type:"+
                "(1)salesman"+
                " | (2)administrator"+
                " | (3)manager"
                );
    }

    //no choosing worker
    public void updateWorkerNoChoosed(long id)
    {

        boolean exit=false;
        Worker worker=workerControl.findWorker(id);
            do{
                if(worker==null) break;
                System.out.println("what do you need to change?: ");
                printChoiceOfChanges();
                int line=listener.getLineInt();
                switch(line){
                    case 1:{
                        String name=getNameUI();
                        worker.setName(name);
                        System.out.println("successfully edited");
                        break;
                    }
                    case 2:{
                        String address=getAddressUI();
                        worker.setAddress(address);
                        System.out.println("successfully edited");
                        break;
                    }
                    case 3:{
                        String phoneNo=getPhoneNoUI();
                        worker.setPhoneNo(phoneNo);
                        System.out.println("successfully edited");
                        break;
                    }
                    case 4:{
                        String password=getPasswordUI();
                        worker.setPassword(password);
                        System.out.println("successfully edited");
                        break;
                    }
                    case 5:{
                        Double salary=getSalaryUI();
                        worker.setSalary(salary);
                        System.out.println("successfully edited");
                        break;
                    }

                    case 6:{
                        exit=true;
                        break;
                    }
                }

            }while(exit==false);
      
    }

    // implamented choosing worker (UI)
    public void updateWorker()
     {
        try{
            long workerId=getWorkerByIDui().getId();

            updateWorkerNoChoosed(workerId);
        }
        catch(Exception e){
            
        }
     }

     public void setDiscountToWorkers()
    {

        //seting type of worker
        printType();
        int type=listener.getLineInt();

        System.out.println("set discount: ");
        double discount=listener.getLineDouble();

        workerControl.setDiscountToWorkers(type, discount);

        System.out.println("Discount successfully changed");
    }

     public String getNameUI()
     {
        System.out.println("name: ");
        String name=listener.getLineString();
        return name;
     }

     public String getAddressUI()
     {
        System.out.println("address:");
        String address=listener.getLineString();
        return address;
     }

     public String getPhoneNoUI()
     {
        System.out.println("phone number: ");
        String phoneNo=listener.getLineString();
        return phoneNo;
     }

     public double getSalaryUI()
     {
        System.out.println("salary: ");
        double salary=listener.getLineDouble();
        return salary;
     }

     public String getPasswordUI()
     {
        System.out.println("password: ");
        String password=listener.getLineString();
        return password;
     }


}
